<?php
namespace Think;
use Think\Log;
class Csv
{
   //导出csv文件
   public function put_csv($list,$title,$csvTitle){
   	$file_name=$csvTitle.".csv";
   	header ( 'Content-Type: application/vnd.ms-excel' );
   	header ( 'Content-Disposition: attachment;filename='.$file_name );
   	header ( 'Cache-Control: max-age=0' );
   	$file =fopen('php://output',"a");
   	$limit=1000;
   	$calc=0;
   	foreach ($title as $v){
   		$tit[]=iconv('UTF-8', 'GB2312//IGNORE',$v);
   	}
   	fputcsv($file,$tit);
   	foreach ($list as $v){
   		$calc++;
   		if($limit==$calc){
   			ob_flush();
   			flush();
   			$calc=0;
   		}
   		foreach ($v as $t){
   			$tarr[]=iconv('UTF-8', 'GB2312//IGNORE',$t);
   		}
   		fputcsv($file,$tarr);
   		unset($tarr);
   	}
   	unset($list);
   	fclose($file);
   	exit();
   }

	/*
	 * 导出csv文件自定义格式的
	 *@param $list  array  需要导出的数据
	 *@param $title array  导出数据的标题
	 *@param $csvTitle string 导出文件的名称
	 * */
	public function put_auto_csv($list,$title,$csvTitle){
		$file_name=$csvTitle.".csv";   //文件名称
		header ( 'Content-Type: application/vnd.ms-excel' );
		header ( 'Content-Disposition: attachment;filename='.$file_name );
		header ( 'Cache-Control: max-age=0' );
		$file =fopen('php://output',"a");
		$limit=300;
		$calc=0;
		foreach ($title as $v){
			$tit[]=iconv('UTF-8', 'GB2312//IGNORE',$v);
		}
		fputcsv($file,$tit);
		//需要导出的字段名称
		$filedList=array('currdate','t_order_num','t_goods_num','t_order_money','t_paid_money','t_refund_money','username','retail_order_type','pay_type');
		//每天的数据
		foreach ($list as $v){
			$calc++;
			if($limit==$calc){
				ob_flush();   //释放php中的缓冲数据
				flush();  //把不在缓冲中的或者说是被释放出来的数据发送到浏览器
				$calc=0;
			}
			foreach ($v as $t_key=>$t){
				if(in_array($t_key,$filedList)){
					if($t_key=='currdate'){
						$t="日期：".date('Y-m-d',$t);
					}
				}else{
					$t='';
				}
				$tarr[]=iconv('UTF-8', 'GB2312//IGNORE',$t);
			}
			fputcsv($file,$tarr);
			unset($tarr);
			//每天的用户数据
			foreach($v['userList'] as $u_item){
				foreach($u_item as $u_key=>$u){
					if(in_array($u_key,$filedList)){
						if($u_key=='username'){
							$u="  用户：".$u;
						}
					}else{ $u=''; }
					$u_arr[]=iconv('UTF-8', 'GB2312//IGNORE',$u);
				}
				fputcsv($file,$u_arr);
				unset($u_arr);
				//每天用户的订单类型
				foreach($u_item['orderTypeList'] as $o_item){
					foreach($o_item as $o_key=>$o){
						if(in_array($o_key,$filedList)){
							if($o_key=='retail_order_type'){
								$o="    订单类型：".$this->getOrderType($o);
							}
						}else{ $o=''; }
						$o_arr[]=iconv('UTF-8', 'GB2312//IGNORE',$o);
					}
					fputcsv($file,$o_arr);
					unset($o_arr);
					//每天用户的订单类型的支付类型
					foreach($o_item['payTypeList'] as $p_item){
						foreach($p_item as $p_key=>$p){
							if(in_array($p_key,$filedList)){
								if($p_key=='pay_type'){
									$p="      支付方式：".$this->getOrderPayType($p);
								}
							}else{ $p=''; }
							$p_arr[]=iconv('UTF-8', 'GB2312//IGNORE',$p);
						}
						fputcsv($file,$p_arr);
						unset($p_arr);
					}
				}
			}
		}
		unset($list);
		fclose($file);
		exit();
	}
	//订单类型
	public function getOrderType($orderType){
		 $str='零售单';
		 if($orderType==1){
			 $str='出口发货单';
		 }
         return $str;
	}
	//订单的支付类型类型
	public function getOrderPayType($payType){
	    switch($payType){
            case 1:$str='现金';break;
            case 2:$str='信用卡';break;
            case 3:$str='EFTPOS';break;
            case 4:$str='支付宝';break;
            case 5:$str='微信';break;
            case 6:$str='银行卡转账';break;
            default:$str='-';
        }
		return $str;
	}
}
?>